﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;


namespace LambdaExtention
{
    static class ExtentionSubstring
    {
        public static StringBuilder Substring(this StringBuilder inputStringBuilder, int start, int lenght) 
        {
            StringBuilder result = new StringBuilder();
            if (start >= 0 && (start + lenght) < inputStringBuilder.Length)
            {
                for (int i = start; i < start + lenght; i++)
                {
                    result.Append(inputStringBuilder[i]);
                }
            }
            else 
            {
                throw new IndexOutOfRangeException("invalid index or length");
            }
            return result;
        }

        public static T Min<T>(this IEnumerable<T> colection) 
        {
            return colection.Min();
        }

        public static T Max<T>(this IEnumerable<T> colection)
        {
            return colection.Max();
        }

        public static dynamic Sum<T>(this IEnumerable<T> colection)
        {
            dynamic result = null;
            foreach (var item in colection)
            {
                result += item;
            }
            return result;
        }

        public static dynamic Average<T>(this IEnumerable<T> colection)
        {
            dynamic result = colection.Sum();
            result /= colection.Count();
            return result;
        }

        public static dynamic Product<T>(this IEnumerable<T> colection)
        {
            dynamic result = null;
            foreach (var item in colection)
            {
                result *= item;
            }
            return result;
        }
    }

    static class ExtentionString 
    {
        public static string CapitalizeFirstLetterOfWorids(this string input) 
        {
            TextInfo textinfo = new CultureInfo("en-Gb", false).TextInfo;
            string result = textinfo.ToTitleCase(input);
            return result;
        }
    
    }
}
